<template>
  <div>
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>品牌管理</el-breadcrumb-item>
        <el-breadcrumb-item>品牌列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-table :data="brandlist" stripe border>
        <el-table-column label="品牌名称" prop="brandname"></el-table-column>
        <el-table-column prop="brandimg" label="品牌图标">
          <template slot-scope="scope">
            <el-image
            style="width: 50px; height: 50px"
            :src="`http://localhost:3000/${scope.row.brandimg}`"></el-image>
          </template>
        </el-table-column>
        <el-table-column prop="brandsort" label="品牌排序"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" @click="getOne(scope.row)"></el-button>
            <el-button type="danger" icon="el-icon-delete" @click="is_del_change(scope.row)"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="query.page"
      :page-sizes="[2, 3, 4, 5]"
      :page-size="query.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      background>
      </el-pagination>
    </el-card>


    <el-dialog
    title="修改品牌"
    :visible.sync="editDialogVisible"
    width="50%">
    <el-form :model="editData" ref="editFormRef" :rules="editFormRules">
      <el-form-item label="品牌名称:" label-width="100px" prop="brandname">
        <el-input v-model="editData.brandname"></el-input>
      </el-form-item>
      <el-form-item label="品牌图片:" label-width="100px">
        <el-upload :action="uploadAction" :headers="{authorization: token}" :show-file-list="false" :on-success="uploadSuccessHandle">
          <el-image
          style="width: 100px; height: 100px"
          :src="`http://localhost:3000/${editData.brandimg}`"
          fit="fill"></el-image>
        </el-upload>
      </el-form-item>
      <el-form-item label="品牌排序:" label-width="100px" prop="brandsort">
        <el-input v-model="editData.brandsort"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="editDialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="editBrand">确 定</el-button>
    </span>
  </el-dialog>
  </div>
</template>

<script>
// 修改无法同步
import {mapState, mapActions} from 'vuex'
export default {
  data(){
    return{
      // 展示参数
      query:{
        page:1,
        pagesize: 2
      },
      // 修改表单对话框打开关闭
      editDialogVisible: false,
      // 修改表单验证
      editFormRules:{
        brandname: [{ required: true, message: '请输入品牌名称', trigger: 'blur' }],
        brandsort: [{ required: true, message: '请输入品牌排序', trigger: 'blur' }]
      },
      // 上传地址
      uploadAction: 'http://localhost:3000/goods/upload',
      // 修改表单数据
      editData: {},
      token : '' || window.sessionStorage.getItem('token')
    }
  },
  methods:{
    // 获取vuex的actions方法
    ...mapActions('brandAbout',['getBrandList','changeIs_del','getOneBrandData','editBrandData']),
    // 监听条目个数变化
    handleSizeChange(newSize){
      this.query.pagesize = newSize
      this.query.page = 1
      this.getBrand()
    },
    // 监听页码变化
    handleCurrentChange(newPage){
      this.query.page = newPage
      this.getBrand()
    },
    // 展示
    getBrand(){
      this.getBrandList(this.query)
    },
    // 将品牌加至回收站
    is_del_change(row){
      this.$confirm('此操作将删除该品牌, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.changeIs_del(row)
          this.getBrand()
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
      
    },
    // 打开修改对话框
    getOne(row){
      this.editData = {...row}
      this.editDialogVisible = true
    },
    // 上传图片
    uploadSuccessHandle(res){
      this.editData.brandimg = res
    },
    // 修改(有bug,未消除)
    editBrand(){
      this.$refs.editFormRef.validate(valid => {
        if (valid){
          this.editBrandData(this.editData)
          this.editDialogVisible = false
          this.getBrand()
        }
      })
    }
  },
  computed:{
    // 获取vuex的state属性
    ...mapState('brandAbout',['brandlist','total'])
  },
  mounted(){
    this.getBrand()
  }
}
</script>

<style>

</style>